<?php
/*
# ------------------------------------------------------------------------
# JA Job Board Package
# ------------------------------------------------------------------------
# Copyright (C) 2004-2010 JoomlArt.com. All Rights Reserved.
# @license GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
# Author: JoomlArt.com
# Websites: http://www.joomlart.com - http://www.joomlancers.com.
# ------------------------------------------------------------------------
*/
function getExternalCombo($field, $selected = "", $edit = false, $view = false, $javascript, $add_empty = 1, $class = '', $mouse_event = '') {
	$r = null;
	
	$user = JFactory::getUser ();
	$db = &JFactory::getDBO ();
	$key = $field->referred_key;
	$value = $field->referred_value;
	$table = $field->referred_table;
	
	if (! $selected && $field->field_name == 'user_id') {
		$selected = $user->id;
	}
	
	if ((($edit && in_array ( $field->display, array ('hide_on_edit', 'hide_on_new' ) )) || $view) && ($user->usertype != 'Aministrator' && $field->display != 'admin_only' || $user->usertype == 'Aministrator') && $field->display != 'hide_all') {
		switch (trim ( $field->field_type )) {
			CASE 'MSELECT' :
				if ($selected) {
					$query = "SELECT $key,$value FROM #__$table WHERE $key IN ($selected)";
					
					$db->setQuery ( $query );
					$objects = $db->loadObjectList ();
					
					$arr_value = array ();
					for($i = 0; $i < count ( $objects ); $i ++) {
						$arr_value [] = $objects [$i]->name;
					}
					$r = implode ( ', ', $arr_value );
				}
				if ($field->field_name == 'location_id'){
					$selected = explode(",", $selected);
					$r = "";					
					foreach ($selected as $k => $str) {
						$str = trim($str);
						$result = show_full_location_tree ( $str );
						$temp = '';
						if (count ( $result ) > 0)
							$temp = implode ( ', ', $result );
						if( $k > 0 )
							$r .= "; ".str_replace(", ", " - ", $temp);
						else 
							$r .= str_replace(", ", " - ", $temp);
					}					
				}
				break;
			
			default :
				if ($field->field_name != 'location_id') {
					$query = "SELECT $key,$value FROM #__$table WHERE $key = '$selected'";
					$db->setQuery ( $query );
					$object = $db->loadObject ();
					if ($object)
						$r = $object->$value;
					else
						$r = 'N/A';
				} else {
					$result = show_full_location_tree ( $selected );
					$temp = '';
					if (count ( $result ) > 0)
						$temp = implode ( ', ', $result );
					$r = $temp;
				}
		}
	
	} elseif (! $edit && $field->display == 'hide_on_new') {
		
		$r = "<input $mouse_event $class type='hidden' name='" . $field->field_name . "' id='" . $field->field_name . "' $javascript value='" . $selected . "' /> ";
	} elseif ($field->display == 'show_all' || ($field->display == 'admin_only' && $user->usertype == 'Aministrator')) {
		$query = "SELECT * FROM #__$table where published = 1";
		$db->setQuery ( $query );
		$objects = $db->loadObjectList ();
		
		$objectHTML = array ();
	
		if ($add_empty)
			$objectHTML [] = JHTML::_ ( 'select.option', "", "-- ".JText::_ (  "Select " . $field->label  ). ' --' );
		
		if ($field->referred_table == 'ja_categories' || $field->referred_table == 'ja_location') {
			$children = array ();
			if ($objects) {
				foreach ( $objects as $v ) {
					$pt = $v->parent_id;
					$list = @$children [$pt] ? $children [$pt] : array ();
					array_push ( $list, $v );
					$children [$pt] = $list;
				}
				if (is_array ( $children )) {
					
					foreach ( $children as $key => $value ) {
						$temp = array ();
						$tmp_children = array ();
						if (is_array ( $children [$key] )) {
							foreach ( $children [$key] as $k => $v )
								$temp [$k] = $v->name;
							asort ( $temp );
							foreach ( $temp as $k => $v )
								$tmp_children [$k] = $children [$key] [$k];
							$children [$key] = $tmp_children;
						}
					}
				
				}
				
				$list = treerecurse ( 0, '', array (), $children );
				if ($list) {
					foreach ( $list as $i => $item ) {
						$treename = $item->treename;
						$treename = JFilterOutput::ampReplace ( $treename );
						$treename = str_replace ( '"', '&quot;', $treename );
						if ($item->id == 0)
							$list [$i]->treename = $treename;
						else
							$list [$i]->treename = $treename;
					}
				}
				$objects = $list;
			}
			if (is_array($objects))
			foreach ( $objects as $key => $v ) {
				$objectHTML [] = JHTML::_ ( 'select.option', $key, $v->treename );
			}
		} else {
			for($i = 0, $n = count ( $objects ); $i < $n; $i ++) {
				$objectHTML [] = JHTML::_ ( 'select.option', $key, $value->treename );
			}
		}
		$field->field_width = ($field->field_width > 150) ? $field->field_width : 150;
		
		switch (trim ( $field->field_type )) {
			case 'MSELECT' :
				if (! is_array ( $selected ))
					if ($selected)
						$selected = explode ( ',', $selected );
				$field->field_width = $field->field_width ? $field->field_width : 150;
				$field->field_height = $field->field_height ? $field->field_height : 200;
				if ($javascript)
					$size = 1;
				else
					$size = 5;
				$r = JHTML::_ ( 'select.genericlist', $objectHTML, $field->field_name . '[]', " $mouse_event $class $javascript  size='$size' multiple='multiple' style=\"width:{$field->field_width}px;height:{$field->field_height}px;\"", 'value', 'text', $selected );
				if (JRequest::getVar('view')=='jajobs')
					$r = str_replace('<option','<option onclick="child_selection(document.getElementById(\''.$field->field_name.'\'),this)"',$r);
				break;
			case 'SELECT' :
				$r = JHTML::_ ( 'select.genericlist', $objectHTML, $field->field_name, "$mouse_event $class $javascript style=\"width:{$field->field_width}px;\"", 'value', 'text', $selected );
				break;
			default :
				$query = "SELECT $key,$value FROM #__$table WHERE $key = '$selected'";
				
				$db->setQuery ( $query );
				$object = $db->loadObject ();
				$field->field_height = $field->field_height ? $field->field_height : 20;
				$field->field_width = $field->field_width ? $field->field_width : 200;
				
				if( isset($object->$value) )
					$r = "<input $mouse_event $class type='text' name='" . $field->field_name . "' id='" . $field->field_name . "' $javascript value='" . $object->$value . "' style=\"width:" . $field->field_width . "px;height:" . $field->field_height . "px;\" /> ";
				else 
					$r = "<input $mouse_event $class type='text' name='" . $field->field_name . "' id='" . $field->field_name . "' $javascript value='' style=\"width:" . $field->field_width . "px;height:" . $field->field_height . "px;\" /> ";
		
		}
		
	// $r = JHTML::_('select.genericlist',   $objectHTML, $field->field_name, 'class="inputbox" size="1"', 'value', 'text', $selected);    
	}
	
	return $r;
}

function rendfield($item, $field, $event = false, $edit = false, $viewonly = false, $view = null,$order_item=null) {
	if (!isset($field->field_name)) return;
	global $option, $mainframe, $jbconfig;
	if ($field->field_type != 'CHECKBOXLIST' && $field->field_type != 'MSELECT') {
		$item->{$field->field_name} = htmlspecialchars_decode($item->{$field->field_name});
	}
	$r = null;
	$url = $mainframe->isAdmin () ? $mainframe->getSiteURL () : JURI::base ();
	
	JHTML::_ ( 'behavior.tooltip' );
	JHTML::_ ( 'behavior.mootools' );
	if (($field->eventmouse != null) && ($field->eventmouse != '')) {
		if ($field->functionjs != null)
			$mouse_event = $field->eventmouse . "='" . $field->functionjs . "'";
		else
			$mouse_event = '';
	} else
		$mouse_event = '';
	if (($field->field_class != null) && ($field->field_class != ''))
		$class = "class='$field->field_class'";
	else
		$class = "class='inputbox'";
	if ($view == null)
		$view = JRequest::getVar ( 'view' );
	$javascript = '';
	if ($event)
	{
		$javascript .= "onchange='form.submit()'";
		
	}
	
	if (isset ( $item->id )&&(int)$item->id!=0)
		$value = $item->{$field->field_name};
	else
		$value = $item->{$field->field_name} ? $item->{$field->field_name} : $field->field_init;
//	$value = str_replace("'"," &rsquo;",$value);
	$user = JFactory::getUser ();
	
	include_once ("components/com_jajobboard/models/jacodes.php");
	$model_codes = new JAJobBoardModelJACodes ( );
	
	$comment = $example = '';
	if ($field->field_comment != '')
		$comment = ' <span class="editlinktip hasTip" title="<b>' . JText::_ ( $field->label ) . '</b>:<br />' . JText::_ ( $field->field_comment ) . '"><img border="0" alt="Tooltip" src="' . $url . '/includes/js/ThemeOffice/tooltip.png"/></span>';
	if ($field->field_example != '')
		$example = JText::_ ( $field->field_example );
	if($viewonly && $field->display=='hide_on_view') return;
	
	if ($field->is_referred) {		
		$r = getExternalCombo ( $field, $value, $edit, $viewonly, $javascript, 1, $class, $mouse_event );
		if (! $viewonly && ! $field->is_readonly)
			$r .= JText::_ ( $comment );
		
		if ( ($field->is_required == '1') && (! $viewonly) && (! $field->is_readonly) && ! $javascript && $field->display != "hide_on_new" ){
			$r .= '<font color="red">*</font>';
		}
		
		if (! $viewonly && $example != '')
			$r .= '<br/>' . JText::_ ( $example );
	} else if ((($edit && in_array ( $field->display, array ('hide_on_edit', 'hide_on_new' ) )) || $viewonly) && ($user->usertype != 'Aministrator' && $field->display != 'admin_only' || $user->usertype == 'Aministrator') && $field->display != 'hide_all') {
		
		switch (trim ( $field->field_type )) {
			CASE 'BOOLEAN' :
				$r = $value ? 'Yes' : 'No';
				break;
			CASE 'IMAGE' :
				if ($value) {
					$src = JURI::base () . $value;
					$temp_path = str_replace ( '\\', DS, JPATH_SITE . DS . $value );
					$temp_path = str_replace ( '/', DS, $temp_path );				
					if (file_exists ( $temp_path )) {
						if(intval($field->field_width)){
							$size = "width='{$field->field_width}'";
							if(intval($field->field_height)){
								$size .= " height='{$field->field_height}'";
							}
						}
						else{
							$imgInfo = getimagesize ( $temp_path );
							$size = "width='{$imgInfo [0]}' height='{$imgInfo [1]}'";
						}
						$r = "<img $class src=\"$src\" alt='photo' $size />";
					}
				}
				if($r=='') {
					if(intval($field->field_width)){
						$size = "width='{$field->field_width}'";
						if(intval($field->field_height)){
							$size .= " height='{$field->field_height}'";
						}
					}
					$src = JURI::base () . 'components/com_jajobboard/images/no-image.gif';
					$r = "<img $class src=\"$src\" alt='photo' $size' />";
				}
				break;
			
			CASE 'URL' :
				if (substr ( $value, 0, 7 ) != 'http://')
					$value = 'http://' . $value;
				$r = "<a target='_blank' href='" . $value . "'>" . $value . "</a>";
				break;
			CASE 'FILE' :
				if ($value) {
					$file_name = explode ( '/', $value );
					if (! $view)
						$view = JRequest::getVar ( 'view', '' );
						
					$ext = strtolower(substr($value, -3, 3));			                       
                    if(!file_exists(JPATH_SITE.DS.'components'.DS.'com_jajobboard'.DS.'images'.DS.'icons'.DS.'applications'.DS.$ext.'.gif')){
                    		$ext = 'unknow';
                    }
                    $file_name = explode('/', $value);
                    $r = '<img src="components/com_jajobboard/images/icons/applications/'.$ext.'.gif" alt="thumbnail"/>';                    
					$r .= "<a href='" . JRoute::_ ( JURI::root().'index.php?option=' . JBCOMNAME . '&view=' . $view . '&task=download&field_name='.$field->field_name.'&cid[]=' . $item->id ) . "'>" . JText::_ (substr($file_name[count($file_name)-1],14) ) . '</a>';
				}
				break;
			CASE 'SELECT' :
				JRequest::setVar ( 'field_id', $field->table_name . '_' . $field->field_name );
				$arr_option = $model_codes->getItems ();
				foreach ( $arr_option as $options ) {
					if ($options->value == $value) {
						$r .= JText::_ ( $options->text );
					}
				}				
				break;
			CASE 'CHECKBOXLIST' :
				JRequest::setVar ( 'field_id', $field->table_name . '_' . $field->field_name );
				$arr_option = $model_codes->getItems ( $value );
				$arr_value = array ();
				foreach ( $arr_option as $options ) {
					$arr_value [] = JText::_ ( $options->text );
				}
				$r = implode ( ',', $arr_value );
				
				break;
			CASE 'MSELECT' :				
				JRequest::setVar ( 'field_id', $field->table_name . '_' . $field->field_name );
				if ( ! empty($value) )
					$arr_option = $model_codes->getItems ( $value );
				else $arr_option = array();
				$arr_value = array ();
				foreach ( $arr_option as $options ) {
					$arr_value [] = JText::_ ( $options->text );
				}
				$r = implode ( ',', $arr_value );
				if ($field->field_name == 'apply_type') {
					$value = explode ( ',', $value );
					if (in_array ( '1', $value ))
						$r .= '&nbsp<span>(<a target="_blank" href="' . $item->direct_url . '">' . $item->direct_url . '</a>)</span>';
				}
				
				break;
			CASE 'RADIO' :
				JRequest::setVar ( 'field_id', $field->table_name . '_' . $field->field_name );
				$arr_option = $model_codes->getItems ( $value );
				$arr_value = array ();
				foreach ( $arr_option as $options ) {
					$arr_value [] = JText::_ ( $options->text );
				}
				$r = implode ( ',', $arr_value );
				
				break;
			CASE 'DATE' :
				
				if (! $value > 0)
					$r = 'N/A';
				else
					$r = generateDate ( $value );
				break;
			CASE 'DATE_CAL' :
				$r = generateDate ( $value );
				break;
			case 'SEPERATOR':
				$r = JText::_($field->label);
				break;
		
		}
		if (! $r && trim ( $field->field_type ) != 'IMAGE') {
			if ( trim ($field->field_type) == "EDITOR" )
				$r = $value;
			else $r = str_replace ( "\n", '<br/>', $value );
		}
		// View permission
		if( $field->display == "show_all_registered" && getCurrentUserType() == "Guest" ){
			$r = "<i style='color:red'>".JText::_("You must")." <a target='_blank' href='".JURI::root()."index.php?option=com_user&view=login'>".JText::_("login")."</a> ".JText::_("to view this information")."</i>";
		}
	} 

	elseif (! in_array ( $field->display, array ('hide_on_new', 'hide_all' ) ) && ($user->usertype != 'Aministrator' && $field->display != 'admin_only' || $user->usertype == 'Aministrator')) {
		
		$readonly = $field->is_readonly ? 'readonly' : '';
		$disabled = $readonly && ! $javascript ? 'disabled' : '';
		
		switch (trim ( $field->field_type )) {
			case 'BLANK' :
				$r = '';
				break;
			case "BOOLEAN" :
				if ($field->field_name=='opt_attachment')
					$mouse_event = "onclick=check(this,'$order_item')";
				$r = JHTML::_ ( 'select.booleanlist', $field->field_name, $class . ' ' . $mouse_event, $value );
				break;
			case "DATE" :
				
				$r = "<input $mouse_event $class name='" . $field->field_name . "' id='" . $field->field_name . "' value='" . $value . "' $javascript>";
				break;
			case "DATE_CAL" :
				
				$dateFormat = $jbconfig['general']->get('format_date');
				if($dateFormat){
					$dateFormat = explode(" ", $dateFormat);
					if( isset ($dateFormat[1]) )
						$dateFormat = $dateFormat[0]." ".str_replace(array("i", "s"), array("M", "S"),$dateFormat[1]);
					else $dateFormat = $dateFormat[0];
				}else $dateFormat = "%Y-%m-%d %H:%M:%S";
								
				if($value)
					$value = date( str_replace(array("%", "M", "S"), array("", "i", "s"), $dateFormat), strtotime($value));
								
				$field->eventmouse = $field->eventmouse ? $field->eventmouse : 'onclick';
				$r = JHTML::_ ( 'calendar', $value, $field->field_name, $field->field_name, $dateFormat, array ('class' => isset ( $field->field_class ) ? $field->field_class : 'inputbox', 'size' => '25', 'maxlength' => '19', $field->eventmouse => $field->functionjs ) );
				break;
			case 'FILE' :
				if (($view == 'jaapplications') && ($field->field_name == 'attachment') && ($item->opt_attachment))
					$input_disabled = 'disabled="disabled"';
				else
					$input_disabled = '';
				if ($value) {
					$file_name = explode ( '/', $value );
					$r = '<span id="download_' . $field->field_name . '">' . "<a target='_blank' href='" . JRoute::_ ( JURI::root().'index.php?option=' . JBCOMNAME . '&view=' . $view . '&task=download&field_name='.$field->field_name.'&cid[]=' . $item->id ) . "'>" . JText::_ ( substr ( $file_name [count ( $file_name ) - 1], 14 ) ) . "</a>" . '</span>&nbsp&nbsp';
					$r .= '<span id="remove_' . $field->field_name . '">(<a href ="javascript:void(0)" onclick="remove_attach(\'' . $field->field_name . '\',\'' . $item->id . '\')">' . JText::_ ( 'Remove' ) . '</a>)</span><br />';
				}
				$r .= "<input $input_disabled $mouse_event $class type='file' name='" . $field->field_name . "' id='" . $field->field_name . "' value='' $javascript /> ";
				$r .= "<br />" . "<i>" . JText::_ ( "Allow ext" ) . ": " . $jbconfig ['general']->get ( 'allowed_ext' ) . ". " . JText::_ ( 'Maximum size' ) . ": " . $jbconfig ["general"]->get ( "max_upload_bytes" ) . "</i>";
				$r .= '<input type="hidden" name="text_' . $field->field_name . '" value="' . $value . '" id="text_' . $field->field_name . '" />';
				break;
			case 'IMAGE' :
				if ($value) {			
					$src = JURI::base () . $value;
					$temp_path = str_replace ( '\\', DS, JPATH_SITE . DS . $value );
					$temp_path = str_replace ( '/', DS, $temp_path );
					if (file_exists ( $temp_path )) {
						if(intval($field->field_width)){
							$size = "width='{$field->field_width}'";
							if(intval($field->field_height)){
								$size .= " height='{$field->field_height}'";
							}
						}
						else{
							$imgInfo = getimagesize ( $temp_path );
							$size = "width='{$imgInfo [0]}' height='{$imgInfo [1]}'";
						}
						
						$r = '<span id="download_' . $field->field_name . '">' . "<img src='" . $src . "' alt='' $size/>" . '</span><br />';
						$r .= '<span id="remove_' . $field->field_name . '">(<a href ="javascript:void(0)" onclick="remove_image(\'' . $field->field_name . '\',\'' . $item->id . '\')">' . JText::_ ( 'Remove' ) . '</a>)</span><br />';
					} else
						$r = "<img src='" . JURI::base () . "/components/com_jajobboard/images/no-image.gif' alt='Default'/>";
				
				} else
					$r = "<img src='" . JURI::base () . "/components/com_jajobboard/images/no-image.gif' alt='Default'/>";
				$r .= "<br/><input $mouse_event $class type='file' name='" . $field->field_name . "' id='" . $field->field_name . "' value=''/>";
				$r .= "<br />" . "<i>" . JText::_ ( "Allow ext" ) . ": " . $jbconfig ['general']->get ( 'allowed_img' ) . ". " . JText::_ ( 'Maximum size' ) . ": " . $jbconfig ["general"]->get ( "max_upload_bytes" ) . "</i>";
				$r .= '<input type="hidden" name="text_' . $field->field_name . '" value="' . $value . '" id="text_' . $field->field_name . '" />';
				break;
			case 'URL' :
			case 'TEXTFIELD' :
			case 'NUMERIC' :
			case 'INTEGER' :
				$field->field_height = $field->field_height ? $field->field_height : 18;
				$field->field_width = $field->field_width ? $field->field_width : 150;
				
				$r = "<input $mouse_event $class type='text' name='" . $field->field_name . "' id='" . $field->field_name . "' $readonly value='" .  $value  . "' style=\"width:" . $field->field_width . "px;height:" . $field->field_height . "px;\" /> ";
				break;
			case 'PASS' :
				$field->field_height = $field->field_height ? $field->field_height : 18;
				$field->field_width = $field->field_width ? $field->field_width : 150;
				$r = "<input $mouse_event  $class type='password' name='" . $field->field_name . "' id='" . $field->field_name . "' $readonly value='' style=\"width:" . $field->field_width . "px;height:" . $field->field_height . "px;\" > ";
				break;
			
			case "EDITOR" :
				// Load the JEditor object
				$editor = & JFactory::getEditor ();
				
				$field->field_height = $field->field_height ? $field->field_height : 200;
				$field->field_width = $field->field_width ? $field->field_width : 300;
				// parameters : areaname, content, width, height, cols, rows, button?					
				$r = $editor->display ( $field->field_name, $value, '100%', $field->field_height, '70', '15', false, array ('theme' => 'simple' ) );
				break;
			case 'MSELECT' :
				JRequest::setVar ( 'field_id', $field->table_name . '_' . $field->field_name );
				$arr_option = $model_codes->getItems ();
				for ($i=0;$i<count($arr_option);$i++)
					$arr_option[$i]->text = JText::_($arr_option[$i]->text);
				if ($field->field_name != 'apply_type') {
					$value = explode ( ',', $value );
					
					$select_options = array ();
					$select_options [0]->value = '';
					$select_options [0]->text = "-- ".JText::_ (  "Select " . $field->label  ). ' --';
					for($i = 0; $i < count ( $arr_option ); $i ++)
						$select_options [] = $arr_option [$i];
					
					$field->field_width = $field->field_width ? $field->field_width : 150;
					$field->field_height = $field->field_height ? $field->field_height : 200;
					$r = JHTML::_ ( 'select.genericlist', $select_options, $field->field_name . '[]', "$mouse_event $class size='4' $javascript multiple='multiple' style=\"width:{$field->field_width}px;height:{$field->field_height}px;\"", 'value', 'text', $value );
				} else {
					if($value && !is_array($value))	$value = explode ( ',', $value );
//					JRequest::setVar ( 'field_id', $field->table_name . '_' . $field->field_name );
//					$arr_option = JobBoardConstant::getApplyJobOptions();
					$enable_option = $jbconfig["posts"]->get('apply_option');
					if ($enable_option)
						$enable_option = explode(',',$enable_option);
					else 
						$enable_option = array('1','2','3');
					$r = '';
					for($i = count ( $arr_option )-1; $i >=0; $i--) 
						if (in_array($arr_option[$i]->value,$enable_option))
						{
							$selected = '';
							$style = "none";	
							if (!is_array($value)){
								if( in_array( 3, $enable_option ) ){
									$value = array( 3 );
								}
								else $value = array( $enable_option[0] );
							}
							if (in_array ( $arr_option [$i]->value, $value )) {
								$selected = 'checked="checked"';
								$style = "";
							}
							
							
							if ($arr_option [$i]->value == 1) {
								$r .= '<input ' . $selected . ' onclick="select_direct_url(this.checked)" type="checkbox" name="apply_type[]" id="apply_type'.$arr_option [$i]->value.'" value="' . $arr_option [$i]->value . '" /><label for="apply_type'.$arr_option [$i]->value.'">' . JText::_($arr_option [$i]->text) . '</label>';							
								$r .= ' <span id="s_direct_url" style="display:'.$style.';vertical-align:top;"><br />';
								$direct_url_field = getItemByFieldName('ja_form_fields','field_name',"'direct_url'");
								for ($ii=0;$ii<count($direct_url_field);$ii++)
									if ($direct_url_field[$ii]->table_name=='ja_jobs')
									{
										$direct_url_field = $direct_url_field[$ii];
										break;
									}
								$r .= rendfield($item,$direct_url_field,false,true);
								$r .= '</span>';
							}
							else{
								$r .= '<input ' . $selected . '  type="checkbox" name="apply_type[]" id="apply_type'.$arr_option [$i]->value.'" value="' . $arr_option [$i]->value . '" /><label for="apply_type'.$arr_option [$i]->value.'">' . JText::_($arr_option [$i]->text) . '</label>';
							}
							
							if ($i >0)
								$r .= '<br />';
						}
				}
				break;
			
			case 'RADIO' :
				JRequest::setVar ( 'field_id', $field->table_name . '_' . $field->field_name );
				$arr_option = $model_codes->getItems ();
				
				$field->field_height = $field->field_height ? $field->field_height : 18;
				$field->field_width = $field->field_width ? $field->field_width : 150;
				
				$r = JHTML::_ ( 'select.radiolist', $arr_option, $field->field_name . '[]', "$mouse_event $class $javascript $disabled size=\"1\" style=\"width:{$field->field_width}px;height:{$field->field_height}px;\"", 'value', 'text', $value );
				break;			
			case 'SELECT' :
				JRequest::setVar ( 'field_id', $field->table_name . '_' . $field->field_name );
				$arr_option = $model_codes->getItems ();
				
				$field->field_height = $field->field_height ? $field->field_height : 25;
				$field->field_width = $field->field_width ? $field->field_width : 150;
				$select_options = array ();
				$select_options [0]->value = '';
				$select_options [0]->text = JText::_ ( "Please select" . ' '. strtolower($field->field_name) . '...');
				for($i = 0; $i < count ( $arr_option ); $i ++)
					$select_options [] = $arr_option [$i];
				$r = JHTML::_ ( 'select.genericlist', $select_options, $field->field_name, "$mouse_event $class $disabled size=\"1\" $javascript style=\"width:{$field->field_width}px;height:{$field->field_height}px;\"", 'value', 'text', $value );
				break;
			
			case 'CHECKBOXLIST' :
				JRequest::setVar ( 'field_id', $field->table_name . '_' . $field->field_name );
				$arr_option = $model_codes->getItems ();
				if ($arr_option) {
					foreach ( $arr_option as $k => $op ) {
						$check = '';
						settype ( $value, 'array' );
						if (in_array ( $op->value, $value ))
							$check = 'checked';
						$r .= "<input $mouse_event $class type='checkbox'  name='$field->field_name[]' id=' " . $field->field_name . $k . "' value='$op->value' $check><label for='$field->field_name.$k '>" . JText::_ ( $op->text ) . "</label><br/>";
					}
				}
				break;
			case 'TEXTAREA' :
				$field->field_height = $field->field_height ? $field->field_height : 5;
				$field->field_width = $field->field_width ? $field->field_width : 60;
				$r = "<textarea $mouse_event  $class cols='$field->field_width' rows='$field->field_height' name='$field->field_name' id='$field->field_name'>$value</textarea>";
				break;
				
			case 'SEPERATOR':
				$r = JText::_($field->label);
				break;
		}
		if (! $viewonly && ! $field->is_readonly)
			$r .= JText::_ ( $comment );
		
		if (($field->is_required == '1') && (! $viewonly) && (! $field->is_readonly) && ! $javascript)
			$r .= '<font color="red">*</font>';
		if (! $viewonly && $example != '')
			$r .= '<br/>' . JText::_ ( $example );
	
	} elseif ($field->display == 'hide_all') {
		$r = "<input $mouse_event $class type='hidden' name='" . $field->field_name . "' id='" . $field->field_name . "' value='" . htmlspecialchars ( $value ) . "'> ";
	}
		
	return $r;
}


function treerecurse($id, $indent, $list, &$children, $maxlevel = 9999, $level = 0, $type = 1) {
	if (@$children [$id] && $level <= $maxlevel) {
		if (is_array ( $children ))
			foreach ( $children [$id] as $v ) {
				$id = $v->id;
				
				if ($type) {
					$pre = '<sup>|_</sup>&nbsp;';
					$spacer = '.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
				} else {
					$pre = '- ';
					$spacer = '&nbsp;&nbsp;';
				}
				
				if ($v->parent_id == 0) {
					$txt = $v->name;
				} else {
					$txt = $pre . $v->name;
				}
				$pt = $v->parent_id;
				$list [$id] = $v;
				$list [$id]->treename = "$indent$txt";
				$list [$id]->children = count ( @$children [$id] );
				$list = treeRecurse ( $id, $indent . $spacer, $list, $children, $maxlevel, $level + 1, $type );
			}
	}
	
	return $list;
}

function rendfield_JS() {
	?>
<script language="javascript" type="text/javascript">
	/* <![CDATA[ */
		urlroot = '<?php
	echo JURI::base ();
	?>';
	<?php 
		$view = JRequest::getVar('view');
		$layout = JRequest::getVar('layout');
		if (($view == 'jajobs') && ($layout=='jaview'))
			$view = 'jaapplications';
	?>
	var view = '<?php echo $view; ?>';
		function remove_attach(field_name,item_id)
		{
			if (!confirm('<?php
	echo JText::_ ( 'Do you want to remove attach file ?' );
	?>'))
				return;
			var url = "index.php?option=com_jajobboard&view=<?php echo $view; ?>&task=remove_attach&field_name="+field_name+"&item_id="+item_id;
			new Ajax(url, {method:'get', onComplete:updateRemoveAttach, data:  { 'text'  : field_name  } }).request();
		}
		function updateRemoveAttach(text)
		{			
			if (text!='')
			{
				document.getElementById('download_'+text).innerHTML = '';
				document.getElementById('text_'+text).value = '';
				document.getElementById('remove_'+text).innerHTML = '';
				if (view=='jaapplications')
				{
					if (document.getElementById('opt_attachment0'))
						document.getElementById('opt_attachment0').checked = true;
					document.getElementById(text).disabled = false;
				}
			}
		}
		
		function remove_image(field_name,item_id)
		{
			if (!confirm('<?php echo JText::_ ( 'Do you want to remove image ?' ); 	?>'))
				return;
			var url = "index.php?option=com_jajobboard&view=<?php echo $view; ?>&task=remove_attach&field_name="+field_name+"&item_id="+item_id;
			new Ajax(url, {method:'get', onComplete:updateRemoveImage, data:  { 'text'  : field_name  } }).request();
		}
		
		function updateRemoveImage(text)
		{
			if (text!='')
			{
				document.getElementById('download_'+text).innerHTML = "<img src='<?php echo JURI::base (); ?>/components/com_jajobboard/images/no-image.gif' alt='Default'/>";
				document.getElementById('text_'+text).value = '';
				document.getElementById('remove_'+text).innerHTML = '';
				if (view=='jaapplications')
				{
					document.getElementById('opt_attachment0').checked = true;
					document.getElementById(text).disabled = false;
				}
			}
		}
	/* ]]> */
	</script>
<?php
}

?>
